//
//  PickerTool.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/22.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit
import SwiftyJSON
import HandyJSON

class PickerTool: UIViewController {
    var handle:SLBlock?
    var datas:[CyrusModel] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        self.closeBtn.isHidden = false
        self.view.backgroundColor = rgba(hex: "#000000", a: 0.7)
        self.titleL.text = ""
        if let title = self.title{
            self.titleL.text = title
        }

        self.nextButton.setTitle("Confirm", for: .normal)
        self.picker.reloadAllComponents()
    }
    class func showPick(list:[JSON],title:String? = "", page:UIViewController,handle: SLBlock?){
        let picker = PickerTool()
        picker.handle = handle
        picker.title = title
        for json in list {
            let dic = json.dictionaryObject
            if let model = CyrusModel.deserialize(from: dic){
                picker.datas.append(model)
            }
        }
        picker.modalPresentationStyle = .overCurrentContext
        picker.modalTransitionStyle = .crossDissolve
        page.present(picker, animated: true)
    }
    lazy var backView:UIView = {
        let pt = create_view(bgColor: .white, supView: self.view) { make in
            make.height.equalTo(381)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
        pt.layer.masksToBounds = true
        pt.layer.cornerRadius = 12
        return pt
    }()
   
    lazy var titleL:UILabel = {
        let pt = create_label(text: "Correct demonstration", textColor: .black,textFont: SLFontConfig.medium18,alignment: .center, supView: self.backView) { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(24)
        }
        pt.numberOfLines = 0
        return pt
    }()

    lazy var closeBtn:UIButton = {
        let pt = create_button(image: IMAGE("icon_return"), supView: self.backView, closure: { make in
            make.top.equalTo(25)
            make.left.equalTo(15)
            make.height.width.equalTo(24)
        }, selector: (target: self, action: #selector(buttonClick), event:.touchUpInside))
        pt.layer.cornerRadius = 12

        
        return pt
    }()
    lazy var nextButton:UIButton = {
        let pt = create_button(title: "", titleColor: .white,titleFont: SLFontConfig.medium18,bgColor: rgba(hex: "#1608EC"), image: nil, supView: self.backView, closure: { make in
            make.bottom.equalTo(-48)
            make.centerX.equalToSuperview()
            make.width.equalTo(281)
            make.height.equalTo(44)
        }, selector: (target: self, action: #selector(buttonClick), event:.touchUpInside))
        pt.layer.cornerRadius = 22
        return pt
    }()
    @objc func buttonClick(btn: UIButton){
        if btn == self.closeBtn{
            self.dismiss(animated: true)
        }else{
            self.dismiss(animated: true)
            let row1 = self.picker.selectedRow(inComponent: 0)
            if let block = self.handle{
                block(row1)
            }
        }
    }
    lazy var picker:UIPickerView = {
        let pt = UIPickerView()
        pt.delegate = self
        pt.dataSource = self
        pt.setValue(UIColor.black, forKey: "textColor")
        self.backView.addSubview(pt)
        pt.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.bottom.equalTo(self.nextButton.snp.top)
            make.top.equalTo(50)
        }
        return pt
    }()
    @objc func datePickerValueChanged(sender: UIDatePicker) {
            let selectedDate = sender.date // 获取选中的日期
            print("Selected date: \(selectedDate)")
            // 你可以在这里处理选中的日期，例如更新UI
        }

}
extension PickerTool: UIPickerViewDelegate,UIPickerViewDataSource{
    //设置选择框的总列数,继承于UIPickViewDataSource协议
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }
        //设置选择框的总行数,继承于UIPickViewDataSource协议
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            //总行数设置为数据源的总长度。component :为0 表示第一列，1 表示第二列
            //根据不同的数据源设置不同的个数
            return self.datas.count
//            if(component == 0){
//                return self.pickerProvincesData.count
//            }else{
//                return self.pickerCitiesData.count
//            }
        }
        //设置选项框各选项的内容,继承于UIPickViewDelegate协议
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
            return self.datas[row].suthat
//            if(component == 0){ //选择第一级数据
//                return self.pickerProvincesData[row]
//            }
//            else{//选择第二级数据
//                return self.pickerCitiesData[row]
//            }
        }
        //选择控件的事件选择
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            if(component == 0){
//                //记录用户选择的值
//                let selectedProvince = self.pickerProvincesData[row] as String
//                // 根据第一列选择的值，获取第二列数据
//                self.pickerCitiesData = self.pickerData[selectedProvince]!
//                //刷新第二列的数据源
//                self.pickerView.reloadComponent(1)
//                //刷新数据源后将第二组数据转到下标为0,并且开启动画效果
//                self.pickerView.selectRow(0, inComponent: 1, animated: true)
            }
        }
    
        //设置每行选项的高度
        func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
            return 44
        }
        // 获取值
        @objc func onclick(_ sender: AnyObject) {
            //获得2列选取值的下标
//            let row1 = self.pickerView.selectedRow(inComponent: 0)
//            let row2 = self.pickerView.selectedRow(inComponent: 1)
//            // 根据下标获取值
//            let selected1 = self.pickerProvincesData[row1] as String
//            let selected2 = self.pickerCitiesData[row2] as String
//            //拼接值
//            let title = String(format: "%@, %@", selected1, selected2)
//            self.label.text = title
        }
}

class CyrusModel:NSObject,HandyJSON{
    var suthat:String = ""
    var transfer: Int = 0

    
    required override init() {

    }
}


